AtCoder Beginner Contest 320

Leyland Number

模拟。

1
2
3
4
5
6
7
8
9
10
11
12
public static void solve() {
int a = io.nextInt(), b = io.nextInt();
int x = 1;
for (int i = 0; i < b; i++) {
x *= a;
}
int y = 1;
for (int i = 0; i < a; i++) {
y *= b;
}
io.println(x + y);
}

Longest Palindrome

模拟。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public static void solve() {
String s = io.next();
int n = s.length();
int ans = 1;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
String a = s.substring(i, j + 1);
String b = new StringBuilder(a).reverse().toString();
if (a.equals(b)) {
ans = Math.max(ans, j - i + 1);
}
}
}
io.println(ans);
}

Slot Strategy 2 (Easy)

暴力枚举每个转盘的时间。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public static void solve() {
int n = 3, m = io.nextInt();
int ans = Integer.MAX_VALUE;
String[] s = new String[n];
for (int i = 0; i < n; i++) {
s[i] = io.next();
}
for (int i = 0; i < n * m; i++) {
char a = s[0].charAt(i % m);
for (int j = 0; j < n * m; j++) {
if (i == j) continue;
char b = s[1].charAt(j % m);
for (int k = 0; k < n * m; k++) {
if (i == k || j == k) continue;
char c = s[2].charAt(k % m);
if (a == b && b == c) {
ans = Math.min(ans, Math.max(i, Math.max(j, k)));
}
}
}
}
io.println(ans == Integer.MAX_VALUE ? -1 : ans);
}

Relative Position

DFS 模拟,需要注意给的不是一棵树,所以在 DFS 时要使用访问数组,而不能用父结点。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
public static void solve() {
int n = io.nextInt(), m = io.nextInt();
List<int[]>[] g = new List[n];
Arrays.setAll(g, k -> new ArrayList<>());
for (int i = 0; i < m; i++) {
int a = io.nextInt() - 1, b = io.nextInt() - 1, x = io.nextInt(), y = io.nextInt();
g[a].add(new int[]{b, x, y});
g[b].add(new int[]{a, -x, -y});
}
long[] X = new long[n];
long[] Y = new long[n];
Arrays.fill(X, Long.MAX_VALUE);
Arrays.fill(Y, Long.MAX_VALUE);
boolean[] vis = new boolean[n];
dfs(0, vis, g, 0, 0, X, Y);
for (int i = 0; i < n; i++) {
if (X[i] != Long.MAX_VALUE && Y[i] != Long.MAX_VALUE) {
io.println(X[i] + " " + Y[i]);
} else {
io.println("undecidable");
}
}
}

private static void dfs(int i, boolean[] vis, List<int[]>[] g, long x, long y, long[] X, long[] Y) {
X[i] = x;
Y[i] = y;
vis[i] = true;
for (int[] t : g[i]) {
int j = t[0];
if (vis[j]) continue;
long nx = t[1] + x, ny = t[2] + y;
dfs(j, vis, g, nx, ny, X, Y);
}
}

Somen Nagashi

还是模拟,可以一边输入一边处理,减少代码量。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public static void solve() {
int n = io.nextInt(), m = io.nextInt();
long[] ans = new long[n], re = new long[n];
PriorityQueue<Integer> q = new PriorityQueue<>();
PriorityQueue<Integer> p = new PriorityQueue<>((a, b) -> Long.compare(re[a], re[b]));
for (int i = 0; i < n; i++) {
q.offer(i);
}
while (m-- != 0) {
int t = io.nextInt(), w = io.nextInt(), s = io.nextInt();
while (!p.isEmpty() && re[p.peek()] <= t) q.offer(p.poll());
if (q.isEmpty()) continue;
int x = q.peek();
ans[x] += w;
re[x] = t + s;
p.offer(q.poll());
}
for (int i = 0; i < n; i++) {
io.println(ans[i]);
}
}
作者

Ligh0x74

发布于

2023-09-17

更新于

2023-09-17

许可协议

评论